package com.example.demo.api;


import com.example.demo.entity.Device;
import com.example.demo.helper.RUtil;
import com.example.demo.service.DeviceService;
import com.example.demo.vo.BTPageDO;
import com.example.demo.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/device")
@Slf4j
public class DeviceRestController {

    @Autowired
    private DeviceService deviceService;

//    /**
//     * 基本分页
//     * @param offset
//     * @param limit
//     * @return
//     */
    @RequestMapping(value = "/get")
    public BTPageDO<Device> list(Integer offset, Integer limit){

        int pageIndex=offset/limit;
        int pageSize=limit;

        Page<Device> myList=deviceService.findByPage(pageIndex,pageSize);

        BTPageDO<Device> stuList=new BTPageDO<Device>();
        stuList.setLimit(pageSize);
        stuList.setOffset(pageIndex);
        stuList.setRows(myList.getContent());
        Long total=myList.getTotalElements();
        stuList.setTotal(total.intValue());

        return  stuList;


    }
//    public R findAll(){
//        Device device = deviceService.findAll();
//
//    }
    @RequestMapping(value = "/list")
    public List<Device> findAll() throws  Exception{
        List<Device> deviceList = deviceService.findAll();
        System.out.println(deviceList);
        return deviceList;

    }


    /**
     * 根据 id取记录
     */
    @GetMapping("/read")
    public R read(Integer id){
        Device device= deviceService.findById(id).get();

        return RUtil.success(device);

    }
//    @GetMapping("/get")
//    public





//    @PutMapping(value = "/update")
//    public R update(Device device){
//
//        deviceService.update(device);
//        return  RUtil.success();
//    }
//
//    @PostMapping(value = "/api/student/add")
//    public R add(Device device){
//        deviceService.add(device);
//        return RUtil.success();
//    }

    @DeleteMapping(value = "/delete")
    public R delete(Integer[] ids){

        Integer mylen= ids.length;
        for (int i = 0; i <mylen ; i++) {

            //读取数据,再删除
            //Device item = deviceService.findById(ids[i]).get();

            deviceService.delete(ids[i]);


        }

        return RUtil.success();

    }



}
